package com.yhyvr.api.controller.file;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.media.upload.UploadResponse;
import com.lz.tool.aliupload.MediaUploadClient;
import com.yhyvr.api.handle.ApiData;
import com.yhyvr.api.service.file.FileUploadService;
import com.yhyvr.api.service.wx.WxService;
import com.yhyvr.api.tool.ViewFactory;
import com.yhyvr.pojo.req.api.ApiReqBody;
import com.yhyvr.pojo.req.api.ApiReqData;
import com.yhyvr.pojo.resp.api.ApiRespData;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;

/**
 * 文件上传
 */
@Controller
public class FileUpAndDown {

	@Resource
	private WxService wxService;
	@Resource
	private FileUploadService apiUploadService;
	/**
	 *
	 */
	@RequestMapping("/api/file/upload")
	public @ResponseBody ModelAndView uploadDb(@ApiData(tokenValidate = false) ApiReqData<ApiReqBody> apiReqBodyApiReqData){
		return ViewFactory.buildApiJsonpView(apiUploadService.upload(apiReqBodyApiReqData),apiReqBodyApiReqData.getCrossDomain());
	}
	@RequestMapping("/api/file/upload/login")
	public @ResponseBody ModelAndView uploadDbLogin(@ApiData(tokenValidate = false) ApiReqData<ApiReqBody> apiReqBodyApiReqData){
		return ViewFactory.buildApiJsonpView(apiUploadService.uploadLogin(apiReqBodyApiReqData),apiReqBodyApiReqData.getCrossDomain());
	}
	/**
	 * 将微信文件放到阿里里服务器
	 * @param serverId
	 * @return
	 */
	@RequestMapping("/api/store/wx/upload")
	public @ResponseBody
	ApiRespData uploadDb(String serverId){
		String wxurl ="http://file.api.weixin.qq.com/cgi-bin/media/get?access_token="+wxService.getJsApiAccessToken()+"&media_id="+serverId;
		URL url = null;
		try {
			String name=System.nanoTime()+".jpg";
			url = new URL(wxurl);
		HttpURLConnection conn = (HttpURLConnection)url.openConnection();
		conn.setRequestMethod("GET");
		conn.setConnectTimeout(5 * 1000);
		InputStream inStream = conn.getInputStream();//通过输入流获取图片数据
			UploadResponse uploadResponse = MediaUploadClient.uploadImg(inStream,name,conn.getContentLengthLong());
			return ApiRespData.buildSucc(uploadResponse);
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (ProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return ApiRespData.buildFail();
	}

	@RequestMapping("/api/file/upload/mu")
	public @ResponseBody ModelAndView uploadDbMu(@ApiData(tokenValidate = false) ApiReqData<ApiReqBody> apiReqBodyApiReqData){
		return ViewFactory.buildApiJsonpView(apiUploadService.uploadMu(apiReqBodyApiReqData),apiReqBodyApiReqData.getCrossDomain());
	}

	@RequestMapping("/api/file/ali/token")
	public @ResponseBody ApiRespData<JSONObject> apiRespBodyApiRespData(@ApiData ApiReqData<ApiReqBody> apiReqBodyApiReqData){
		return apiUploadService.getBaiChuanToken(apiReqBodyApiReqData);
	}
}
